package com.sojpt.boot.controller.business;

import java.util.*;

import com.jfinal.aop.Before;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.sojpt.boot.controller.business.validator.LkyClassAddValidator;
import com.sojpt.boot.controller.business.validator.LkyClassUpdateValidator;
import com.sojpt.boot.intercepter.admin.AdminLoginInterceptor;
import com.sojpt.boot.intercepter.admin.AdminPopedomInterceptor;
import com.sojpt.boot.ui.iview.IViewController;
import com.sojpt.kit.SoJLogKit;
import com.sojpt.model.*;
import com.sojpt.model.bo.LkyTeacherClassRelBO;
import com.sojpt.util.ResultUtil;
import org.springframework.web.bind.annotation.*;


/**
 *
 * @author Sohnny
 *
 */
@RestController
@RequestMapping("/admin/class/")
//@Before({AdminLoginInterceptor.class, AdminPopedomInterceptor.class})
public class LkyClassAdminController extends IViewController {

    @GetMapping("list")
    public void list() {
        LkyClass lkyClass = getModel(LkyClass.class, "lkyClass");
        SqlPara sqlPara = Db.getSqlPara("lky_class.selectClass", getKv(lkyClass));
        Page<Record> page = Db.paginate(pageNumber(), pageSize(), sqlPara);
        renderJson(page);
    }

    @Before(LkyClassAddValidator.class)
    @PostMapping("save")
    public void save() {
        DicPopedom dicPopedom = new DicPopedom();
        dicPopedom.setForwardAction("/admin/class/save");
        LkyClass lkyClass = getModel(LkyClass.class, "lkyClass");
        lkyClass.setId(UUID.randomUUID().toString().replace("-", ""));
        lkyClass.setCreationTime(new Date());
        lkyClass.setUpdateTime(new Date());
        if(lkyClass.save()) {
            renderJson(Ret.ok());
            dicPopedom.setPopedomName("添加成功");
        } else {
            renderJson(Ret.fail());
            dicPopedom.setPopedomName("添加成功");
        }
        SoJLogKit.writeLog(this, dicPopedom,getSessionUser());
    }

    @Before(LkyClassUpdateValidator.class)
    @PostMapping("update")
    public void update() {
        DicPopedom dicPopedom = new DicPopedom();
        dicPopedom.setForwardAction("/admin/class/save");
        String id=getPara("lkyClass.id");
        LkyClass lkyClass = getModel(LkyClass.class, "lkyClass",false);
        if(lkyClass.update()) {
            renderJson(Ret.ok());
            dicPopedom.setPopedomName("修改成功");
        } else {
            renderJson(Ret.fail());
            dicPopedom.setPopedomName("修改成功");
        }
        SoJLogKit.writeLog(this, dicPopedom,getSessionUser());
    }

    @PostMapping("delete")
    public void delete() {
        DicPopedom dicPopedom = new DicPopedom();
        dicPopedom.setForwardAction("/admin/class/save");
        LkyClass lkyClass = getModel(LkyClass.class, "lkyClass");
        if(LkyClass.dao.deleteById(lkyClass.getId())) {
            renderJson(Ret.ok());
            dicPopedom.setPopedomName("删除成功");
        } else {
            renderJson(Ret.fail());
            dicPopedom.setPopedomName("删除成功");
        }
        SoJLogKit.writeLog(this, dicPopedom,getSessionUser());
    }

    @PostMapping("batchDelete")
    public void batchDelete() {
        DicPopedom dicPopedom = new DicPopedom();
        dicPopedom.setForwardAction("/admin/class/batchDelete");
        String[] ids = getPara("ids").split(",");
        SqlPara sqlPara = Db.getSqlPara("lky_class.deleteByIds", Kv.by("idList", Arrays.asList(ids)));
        if(Db.update(sqlPara) > 0) {
            renderJson(Ret.ok());
            dicPopedom.setPopedomName("批量删除成功");
        } else {
            renderJson(Ret.fail());
            dicPopedom.setPopedomName("批量删除失败");
        }
        SoJLogKit.writeLog(this, dicPopedom,getSessionUser());
    }


    // 查询查询
    @GetMapping("listFind")
    public void listFind() {
        LkyClass lkyClass = getModel(LkyClass.class, "lkyClass");
        String coid =getPara("coid");
        Map<String,String> map =new HashMap<>();
        map.put("name",lkyClass.getName());
        map.put("classNumber",lkyClass.getClassNumber());
        map.put("mid",lkyClass.getMid());
        map.put("coid",coid);
        SqlPara sqlPara = Db.getSqlPara("lky_class.selectFind", map);
        Page<Record> page = Db.paginate(pageNumber(), pageSize(), sqlPara);
        renderJson(page);
    }

    // 老师分配班级模块，传回专业id，查询班级
    @PostMapping("queryFindClassByMid")
    public void queryFindClassByMid() {
        String mid = getPara("mid");
        SqlPara sqlPara = Db.getSqlPara("lky_class.queryFindClassByMid", Kv.by("mid", mid));
        List<LkyClass> lkyClass = LkyClass.dao.find(sqlPara);
        renderJson(ResultUtil.success(lkyClass));
    }

    // 查看老师管理的班级
    @PostMapping("queryFindClassMajorCollegeListByTid")
    public void queryFindClassMajorCollegeListByTid() {
        String tid = getPara("tid");
        SqlPara sqlPara = Db.getSqlPara("lky_class.queryFindClassMajorCollegeListByTid", Kv.by("tid", tid));
        List<LkyClass> lkyClass = LkyClass.dao.find(sqlPara);
        renderJson(ResultUtil.success(lkyClass));
    }

    @PostMapping("saveSC")
    public void saveSC(LkyTeacherClassRelBO lkyTeacherClassRelBO) {
        DicPopedom dicPopedom = new DicPopedom();
        dicPopedom.setForwardAction("/admin/class/saveSC");
        LkyClassTeacher lkyClassTeacher = new LkyClassTeacher();
        lkyClassTeacher.setTid(lkyTeacherClassRelBO.getTid());
        List<String> classTeacherList = lkyTeacherClassRelBO.getClassTeacherList();
        for (String cid : classTeacherList) {
            lkyClassTeacher.setId(UUID.randomUUID().toString().replace("-", ""));
            lkyClassTeacher.setCid(cid);
            if(lkyClassTeacher.save()) {
                renderJson(Ret.ok());
                dicPopedom.setPopedomName("添加成功");
            } else {
                renderJson(Ret.fail());
                dicPopedom.setPopedomName("添加成功");
            }
        }
        SoJLogKit.writeLog(this, dicPopedom,getSessionUser());
    }

}